package com.example.holes;

import java.util.ArrayList;

import android.graphics.Bitmap;

public class Mole03_Porcupine extends Mole {
	private boolean raisingSpike = false, loweringSpike = false, spikes = false;
	
	public Mole03_Porcupine(int holeNum, ArrayList<Bitmap> images, int imageIndex, float timeMod){
		super(holeNum, images, imageIndex);
		setStats(timeMod);
	}
	
	private void setStats(float timeMod){
		health = 1;
		lifespan = (int)(Global.UPS * 4 *timeMod);
		attack = 10;
		points = 25;
	}

	@Override
	public int timeUp() {
		if (lifespan == 0)
			return 0; // yes
		else
			return -1; // no 
	}
	
	@Override
	// returns true if dead
	public int attack(int attack){
		if (spikes == false){ // only damage when spikes are down
			health -= attack;
			if (health <= 0)
				return 1; // killed
			else
				return 0; // attacked
		}
		else
			return 3; // spikes were up
	}
	
	@Override
	public void setImage(ArrayList<Bitmap> images, int imageIndex){
		mImages = new Bitmap[6];
		
		for (int i = 0; i < 6; i++){
			mImages[i] = images.get(imageIndex + i);
			// 2nd row: 80% width, 80% height
			if (row == 2){
				mImages[i] = Bitmap.createScaledBitmap(mImages[i],(int)(mImages[i].getWidth()*0.80), (int)(mImages[i].getHeight()*0.80), false);
			}
			// 3rd row: 60% width, 60% height
			else if (row == 3){
				mImages[i] = Bitmap.createScaledBitmap(mImages[i],(int)(mImages[i].getWidth()*0.60), (int)(mImages[i].getHeight()*0.60), false);
			}
		}
		orig = mImages[0];
	}
	
	// ==============
	//   v UPDATE v
	// ==============
	@Override
	public void Update(){
		if (start){ // start is set by Hole only
			lifespan--;
			// check rising/lowering
			lifespanimations();
			
			// check spikes
			spikeanimations();
		}
	}
	
	// handles queueing the raising and lowering spike animations
	private void spikeanimations(){
		if (lifespan == 115 || lifespan == 45){
			raisingSpike = true;
			spikes = true;
		}
		else if (lifespan == 90 || lifespan == 20){
			loweringSpike = true;
		}
			
		if (raisingSpike)
			raiseSpikeAnimation();
		
		else if (loweringSpike)
			lowerSpikeAnimation();
	}
	// ==============
	//   ^ UPDATE ^
	// ==============

	// ==============
	//     DRAW in super
	// ==============
	private void nextFrameSpikes(){
		image = mImages[frame - 5]; // change frame
		int topheight = (int)(top + height);
		// crop image
		if ((topheight) > ycutoff){
			image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), height - (topheight - ycutoff));
		}
	}
	
	private void raiseSpikeAnimation(){
		animateTimer--;
		if (animateTimer == 0){
			frame++;
			animateTimer = TIME_BETWEEN;
			nextFrameSpikes();
			if (frame == 10){
				raisingSpike = false;
			}
		}
	}
	
	private void lowerSpikeAnimation(){
		animateTimer--;
		if (animateTimer == 0){
			frame--;
			animateTimer = TIME_BETWEEN;
			nextFrameSpikes();
			if (frame == MAX_FRAMES){
				loweringSpike = false;
				spikes = false;
			}
		}
	}
	
	// gives the position of the mole's image from input hole number
	@Override
	public void setImagePosition(int holeNum){
		switch (holeNum) {
			case 0:	row = 1;
					left = 153;
					top = 292;
					break;
			case 1:	row = 1;
					left = 334;
					top = 292;
					break;
			case 2:	row = 1;
					left = 515;
					top = 292;
					break;
			case 3:	row = 2;
					left = 188;
					top = 186;
					break;
			case 4:	row = 2;
					left = 347;
					top = 186;
					break;
			case 5:	row = 2;
					left = 506;
					top = 186;
					break;
			case 6:	row = 3;
					left = 216;
					top = 113;
					break;
			case 7:	row = 3;
					left = 360;
					top = 113;
					break;
			case 8:	row = 3;
					left = 502;
					top = 113;
					break;
		}
	}
}
